﻿using Architecture.Domain.Service;
using Architecture.Repository.Impl;
using Architecture.Repository.Interface;
using Architeture.Domain;
using Architeture.Domain.DTO;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.ZhuBoLin.Application.Command.RoleCommand;
using RBAC.ZhuBoLin.Application.Command.RoleCommand.Farminginfo;
using RBAC.ZhuBoLin.Application.Command.RoleCommand.Goods;
using RBAC.ZhuBoLin.Application.Command.RoleCommand.Healthinfo;
using RBAC.ZhuBoLin.Application.CommandHandlers.RoleHandler.Healthinfo;
using RBAC.ZhuBoLin.Dtos;

namespace RBAC.ZhuBoLin.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class GoodsController : ControllerBase
    {
        protected readonly IMediator mediator;
        protected readonly IEnterpriseService enterpriseService;

        public GoodsController(IMediator mediator, IEnterpriseService enterpriseService)
        {
            this.mediator = mediator;
            this.enterpriseService = enterpriseService;
        }


        /// <summary>
        /// 商品显示
        /// </summary>

        [HttpGet]
        public async Task<ApiResult<GoodsPagedto>> GetGoodsInfo([FromQuery] GoodsSelectCommand sd)
        {
            ApiResult<GoodsPagedto> result = new ApiResult<GoodsPagedto>();
            try
            {
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 溯源删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GoodsInfoDel(GoodsDeleteCommand dd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(dd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 溯源详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        /// 

        [HttpGet]
        public async Task<ApiResult<GoodsInfo>> SetGoods([FromQuery] GoodsSetCommand sd)
        {
            ApiResult<GoodsInfo> result = new ApiResult<GoodsInfo>();
            try
            {
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 溯源添加
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> GoodsAdd(GoodsCreateCommadn cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        [AllowAnonymous]
        public string Upload(IFormFile file)
        {
            string savePath = Directory.GetCurrentDirectory() + @"\wwwroot\pic\";
            string newFileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            using (FileStream fs = new FileStream(savePath + newFileName, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            return newFileName;
        }

        /// <summary>
        /// 溯源修改
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GoodsUpd(GoodsUpdateCommand ud)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(ud);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 图片表添加
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> AddPic(PicCreateCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 获取图片表
        /// </summary>
        /// <param name="sd"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<List<AllPic>>> GetProductinspectionreport([FromQuery] PicSelectCommand sd)
        {
            ApiResult<List<AllPic>> result = new ApiResult<List<AllPic>>();
            try
            {
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除图片表
        /// </summary>
        /// <param name="pd"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> DeletePic(PicDeleteCommand pd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(pd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 养殖添加
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> farmingAdd(FarmingCreateCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 养殖显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<Farming>> farmingList([FromQuery] FarmingSelectCommand sd)
        {
            ApiResult<Farming> result = new ApiResult<Farming>();
            try
            {
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 保健添加
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        /// 
        [HttpPost]

        public async Task<ApiResult<int>> HealthAdd(HealthCreateCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 保健显示
        /// </summary>
        /// <param name="sd"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<List<HealthCare>>> GetHealthCare([FromQuery] HealthSelectCommand sd)
        {
            ApiResult<List<HealthCare>> result = new ApiResult<List<HealthCare>>();
            try
            {
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 保健删除
        /// </summary>
        /// <param name="dd"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> HealthDel(HealthCarCommand dd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(dd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 公司添加
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        /// 
        [HttpPost]

        public async Task<ApiResult<int>> EnterpriseAdd(EnterpriseInfo a)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await enterpriseService.EnterpriseAdd(a);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 公司显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<EnterpriseInfo>> GetEnt(int id)
        {
            ApiResult<EnterpriseInfo> result = new ApiResult<EnterpriseInfo>();
            try
            {
                result.Data = await enterpriseService.GetEnt(id);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填企业信息
        /// </summary>
        /// <param name="ed"></param>
        /// <returns></returns>
        /// 
        [HttpGet]

        public async Task<ApiResult<EnterpriseInfo>> SetEntinfo([FromQuery] EnterSetCommand ed)
        {
            ApiResult<EnterpriseInfo> result = new ApiResult<EnterpriseInfo>();
            try
            {
                result.Data = await mediator.Send(ed);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改屠宰企业信息
        /// </summary>
        /// <param name="sd"></param>
        /// <returns></returns>
        /// 
        [HttpPost]

        public async Task<ApiResult<int>> EntRlaughterAdd(EnterAddOneCommand sd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 公司信息显示
        /// </summary>
        /// <param name="sd"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<List<EnterDTO>>> GetEnterDTO([FromQuery] EnterSelectdtoCommand sd)
        {
            ApiResult<List<EnterDTO>> result = new ApiResult<List<EnterDTO>>();
            try
            {
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 保健信息修改
        /// </summary>
        /// <param name="ud"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> HealthUpd(HealthUpdateCommand ud)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(ud);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
